package com.hyb.controller;


import com.alibaba.fastjson.JSON;
import com.hyb.entity.Product;
import com.hyb.page.PageReq;
import com.hyb.response.Result;
import com.hyb.service.ProductService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Set;

/**
 * <p>
 *  产品Controller
 * </p>
 *
 * @author hyb
 * @since 2021-12-29
 */
@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductService productService;

    @RequestMapping(value = "/saveProduct",method = RequestMethod.POST)
    @ApiOperation("保存产品信息")
    public String saveProduct(@RequestBody Product product){
        productService.createProduct(product);
        return JSON.toJSONString(new Result().setCode(200).setMessage("保存成功"));
    }

    @RequestMapping(value = "/loadProducts",method =RequestMethod.GET)
    @ApiOperation("获取所有产品信息")
    public String loadProducts(Integer limit, Integer current, String productType, String productSeries, String productBatch){
        PageReq products = productService.getProducts(limit, current, productType, productSeries, productBatch);
        return JSON.toJSONString(new Result().setMessage("加载成功").setCode(200).setData(products));
    }

    @RequestMapping(value = "/loadProductById",method = RequestMethod.GET)
    @ApiOperation("通过Id获取产品信息")
    public String loadProductById(Integer productId){
        Product productById = productService.getProductById(productId);
        return JSON.toJSONString(new Result().setCode(200).setMessage("加载成功").setData(productById));
    }

    @RequestMapping(value = "/updateProduct",method = RequestMethod.PUT)
    @ApiOperation("修改产品信息")
    public String updateProduct(@RequestBody Product product){
        productService.updateProduct(product);
        return JSON.toJSONString(new Result().setCode(200).setMessage("修改成功"));
    }


    @RequestMapping(value = "/deleteProduct",method = RequestMethod.DELETE)
    @ApiOperation("删除产品信息")
    public String deleteProduct(Integer productId){
        productService.deleteProduct(productId);
        return JSON.toJSONString(new Result().setCode(200).setMessage("删除成功"));
    }

    @RequestMapping(value = "/loadProductSeriesByProductType",method = RequestMethod.GET)
    @ApiOperation("通过产品类型获取产品类别")
    public String loadProductSeriesByProductType(String productType){
        Set<Product> products = productService.selectProductSeries(productType);
        return JSON.toJSONString(new Result().setMessage("加载成功").setCode(200).setData(products));
    }

    @RequestMapping(value = "/loadProductModelsByProductSeries",method = RequestMethod.GET)
    @ApiOperation("通过产品类别获取产品型号")
    public String loadProductModelsByProductSeries(String productSeries){
        List<Product> products = productService.selectProductModels(productSeries);
        return JSON.toJSONString(new Result().setMessage("加载成功").setCode(200).setData(products));
    }

    @RequestMapping(value = "/ProductTypeAndProductSeriesAndProductModel",method = RequestMethod.GET)
    @ApiOperation("通过产品系列、产品类型和产品型号查询产品")
    public String ProductTypeAndProductSeriesAndProductModel(String productType,String productSeries ,String productModel){
        Product product = productService.getProductInfoByProductTypeAndProductSeriesAndProductModel(productType, productSeries, productModel);
        return JSON.toJSONString(new Result().setData(product).setMessage("查询成功").setCode(200));
    }
}
